package com.lyl.momanapp.util.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 这是一个斜对的按钮
 */
public class PolygonImageview extends ImageView {

    private int width = -1;
    private int height = -1;
    private Bitmap bitmap;

    private int mwidth;
    private int mheight;

    public PolygonImageview(Context context) {
        super( context);
    }

    public PolygonImageview(Context context, AttributeSet attrs, int defStyle) {
        super( context, attrs, defStyle);
    }

    public PolygonImageview(Context context, AttributeSet attrs) {
        super( context, attrs);
    }

    Bitmap newBitmap;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if(action != MotionEvent.ACTION_DOWN) {
            return super.onTouchEvent( event);
        }
        int x = (int)event.getX();
        int y = (int)event.getY();
        if(width == -1 || height == -1) {
            Drawable drawable = getBackground().getCurrent();
            bitmap = ((BitmapDrawable)drawable).getBitmap();
            width = bitmap.getWidth();
            height = bitmap.getHeight();

            float scalyNumX;
            float scalyNumY;

            scalyNumX = (float)mwidth/width;
            scalyNumY = (float)mheight / height;


            Matrix matrix = new Matrix();
            matrix.postScale(scalyNumX, scalyNumY); //长和宽放大缩小的比例

            newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(),matrix,true );

        }
        int bx=newBitmap.getWidth();
        int by= newBitmap.getHeight();
        if( x < 0 || y < 0 || x >= bx || y >= by) {
            return false;
        }
        //if(x<newBitmap.getWidth()&&y<newBitmap.getHeight()){

        int pixel = newBitmap.getPixel( x, y);
        if(Color.TRANSPARENT == pixel) {
            return false;
        }
        /*}else{
            return false;
        }*/
        return super.onTouchEvent( event);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 这里要计算一下控件的实际大小，然后调用setMeasuredDimension来设置
        mwidth = this.getMeasuredSize(widthMeasureSpec, true);
        mheight = this.getMeasuredSize(heightMeasureSpec, false);
        setMeasuredDimension(mwidth, mheight);
    }

    /**
     * 计算控件的实际大小
     * @param length onMeasure方法的参数，widthMeasureSpec或者heightMeasureSpec
     * @param isWidth 是宽度还是高度
     * @return int 计算后的实际大小
     */
    private int getMeasuredSize(int length, boolean isWidth){
        Drawable drawable = getBackground().getCurrent();
        bitmap = ((BitmapDrawable)drawable).getBitmap();
        // 模式
        int specMode = MeasureSpec.getMode(length);
        // 尺寸
        int specSize = MeasureSpec.getSize(length);
        // 计算所得的实际尺寸，要被返回
        int retSize;
        // 得到两侧的padding（留边）
        int padding = (isWidth? getPaddingLeft()+getPaddingRight():getPaddingTop()+getPaddingBottom());

        // 对不同的指定模式进行判断
        if(specMode== MeasureSpec.EXACTLY){  // 显式指定大小，如40dp或fill_parent
            retSize = specSize;
        }else{                              // 如使用wrap_content
            retSize = (isWidth? bitmap.getWidth()+padding : bitmap.getHeight()+padding);
            if(specMode== MeasureSpec.UNSPECIFIED){
                retSize = Math.min(retSize, specSize);
            }
        }
        return retSize;
    }
}